﻿using WF_AppFuel.Validation.Attributes;
using WF_AppFuel.Validation.Base;

namespace WF_AppFuel.Validation.Engines
{
    /// <summary>
    /// This engine is used for validating alphanumeric fields
    /// </summary>
    /// <author>Arjay C. Nacion</author>
    public sealed class IsAlphaNumericValidationEngine : BaseValidationEngine
    {
        public override bool Run()
        {
            IsAlphaNumeric attrib = (IsAlphaNumeric)ValidationAttribute;
            bool isWhiteSpaceAllowed = attrib.IsWhiteSpaceAllowed;

            if (Value == null)
                return false;

            if (string.IsNullOrEmpty(Value.ToString()))
                return false;

            foreach (char c in Value.ToString())
            {
                if (isWhiteSpaceAllowed && char.IsWhiteSpace(c))
                    continue;

                if (!(char.IsLetter(c) && char.IsNumber(c)))
                    return false;
            }
            return true;
        }
    }
}
